package com.imau.jdbc;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {

    private  static  String url;
    private static  String user ;
    private static  String password;
    private static  String driver;
    private static Connection connection;


    static {

        //1.创建Properties对象
        Properties properties = new Properties();

        //2.加载配置文件
        try {
            properties.load(new FileInputStream("D:\\IDE_Project\\javaee20252\\maven-01\\src\\main\\resources/jdbc.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //3.获取配置文件中的参数值

        url = properties.getProperty("url");
        user =properties.getProperty("user");
        password =properties.getProperty("password");
        driver =properties.getProperty("driver");

    }

    public  static Connection getConnection()
    {
        if(connection !=null)
            return  connection;

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return  connection;
    }
}
